package com.lxp.tksys.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

//元数据处理对象
@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override  //插入时自动填充
    public void insertFill(MetaObject metaObject) {
        log.info(metaObject.toString());
        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("createUser", BaseContext.getCurrentId());    //获取当前的id
        metaObject.setValue("updateUser", BaseContext.getCurrentId());
    }

    @Override  //更新时自动填充
    public void updateFill(MetaObject metaObject) {
        log.info(metaObject.toString());

        long id = Thread.currentThread().getId();  // 看看是不是同一个id
        log.info("线程id：{}", id);
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("updateUser", BaseContext.getCurrentId());
    }
}
